/**
 * 
 */
package com.jie.eclipse.pan.editors;

import org.eclipse.core.internal.resources.Folder;
import org.eclipse.core.internal.resources.Workspace;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;
import org.eclipse.ui.dialogs.FileSelectionDialog;
import org.eclipse.ui.dialogs.FileSystemElement;
import org.eclipse.ui.dialogs.ResourceListSelectionDialog;
import org.eclipse.ui.dialogs.ResourceSelectionDialog;
import org.eclipse.ui.dialogs.SelectionDialog;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.part.FileEditorInput;

import com.jie.eclipse.pan.utils.Utils;


/**
 * @author yanjie
 *
 */
public class BaseFormEditor extends FormEditor implements IResourceChangeListener{
	public FileEditorInput uiInput = null;
	public FileEditorInput backInput = null;
	public FileEditorInput oldInput = null;
	

	public void resourceChanged(IResourceChangeEvent arg0) {
	
	}
	
	@Override
	protected void addPages() {
		
	}
	@Override
	public void doSave(IProgressMonitor arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}
	
	@Override
	public String getPartName() {
		FileEditorInput fi = (FileEditorInput)getEditorInput();
		return fi.getName().replace(".pll", "");
	}
	
	public FileEditorInput getBackInput() {
		if(this.backInput == null){
			FileEditorInput fi = (FileEditorInput)getEditorInput();
			String uiJsp = fi.getName().replace(".pll", "_backend.jsp");
			IFolder f1 = (IFolder)fi.getFile().getParent();
			this.backInput = new FileEditorInput(f1.getFile(uiJsp));
		}
		return backInput;
	}
	public void setBackInput(FileEditorInput backInput) {
		this.backInput = backInput;
	}
	public FileEditorInput getUiInput(){
		if(this.uiInput == null){
			FileEditorInput fi = (FileEditorInput)getEditorInput();
			
			String uiJsp = fi.getName().replace(".pll", ".jsp");
			IFolder f1 = (IFolder)fi.getFile().getParent();
			this.uiInput = new FileEditorInput(f1.getFile(uiJsp));
		}
		return uiInput;
	}
	public void setUiInput(FileEditorInput uiInput) {
		this.uiInput = uiInput;
	}
	public FileEditorInput getOldInput() {
		if(oldInput ==null){
			FileEditorInput fi = (FileEditorInput)getEditorInput();
			String uiJsp = fi.getName().replace(".pll", ".jsp").replace("_backend", "");
			IFolder f1 = (IFolder)fi.getFile().getParent();
			
			String path = f1.getLocationURI().toString().replace("file:/", "");
			String path1 = path.substring(0,path.lastIndexOf("/")+1);
			String path2 = path.substring(path.lastIndexOf("/")+1,path.length());
			
			if(path2.startsWith("jm")){
				try{
					path2 = path2.replace("jm", "").toLowerCase();
					Path ipath = new Path(path1+path2+"/"+uiJsp);
				    IFile file = f1.getWorkspace().getRoot().getFileForLocation(ipath);
				    if(file.exists()){
				    	this.oldInput = new FileEditorInput(file);
				    }
				}catch(Exception e){
					return null;
				}
			}
			
		}
		return oldInput;
	}

	public void setOldInput(FileEditorInput oldInput) {
		this.oldInput = oldInput;
	}
	
	public void doNextPage(){
		int curPage = this.getActivePage();
		if(this.getActivePage() < this.getPageCount()-1){
			curPage++;
		}else{
			curPage = 0;
		}
		this.setActivePage(curPage);
		
	}
	
	public void doPrePage(){
		int curPage = this.getActivePage();
		if(this.getActivePage() - 1 >= 0){
			curPage--;
		}else{
			curPage = this.getPageCount()-1;
		}
		this.setActivePage(curPage);
		
	}
	
	/**
	 * 打开old Jsp
	 */
	public void openOldPage(){
		FileEditorInput fi = (FileEditorInput)getEditorInput();
		ResourceListSelectionDialog dialog = new ResourceListSelectionDialog	(
				this.getSite().getShell(),fi.getFile().getProject(),1);
		
		if (dialog.open() == ContainerSelectionDialog.OK) {
			Object[] result = dialog.getResult();
			if(result.length == 0) return;
			Object obj  = result[0];
			if(obj instanceof IFile){
				if(this.getOldInput() == null){
					this.setOldInput(new FileEditorInput((IFile)obj));
					if(this instanceof PLLFormEditor){
						PLLFormEditor pll = (PLLFormEditor)this;
						pll.createPage3();
						pll.setActiveEditor(pll.getOldEditor());
					}else if (this instanceof PLLJspFormEditor){
						PLLJspFormEditor pll = (PLLJspFormEditor)this;
						pll.createPage2();
						pll.setActiveEditor(pll.getOldEditor());
					}
				}else{
					this.setOldInput(new FileEditorInput((IFile)obj));
					if(this instanceof PLLFormEditor){
						PLLFormEditor pll = (PLLFormEditor)this;
						pll.getOldEditor().setInput(this.getOldInput());
						pll.setActiveEditor(pll.getOldEditor());
					}else if (this instanceof PLLJspFormEditor){
						PLLJspFormEditor pll = (PLLJspFormEditor)this;
						pll.getOldEditor().setInput(this.getOldInput());
						pll.setActiveEditor(pll.getOldEditor());
					}
				}
			}
		}
	}
}
